<?php
namespace Admin\Controller;
use Think\Controller;
use Think\Page;
	class PhotosController extends CommonController{
		
		
		public function getlist(){
			$id = $_GET['id'];
			$flag = $_GET['flag'];
			$m_flag = ucfirst($flag);
			$m = M($m_flag);
			
			$map = array(
				'photoable'=>$flag,
				'photoable_id'=>$id,
			); 
    		$parameter = array(
    			'id'=>$id,
    			'flag'=>$flag,
    		);
			
			if($flag=='layout'){
				$layout = $m->find($id);
				$this->assign('layout',$layout);
				
				$info = M('House')->where('id='.$layout['house_id'])->getField('name');
			}else if($flag=='house') {
				$info = $m->where('id='.$id)->getField('name');
				$alblist = M('Album')->getField('id,name');
				$this->assign('alblist',$alblist);
				
				if($_GET['album_id']){
					$album_id = $_GET['album_id'];
				}else{
					$album_id = M('Album')->getField('id',1);
				}
				$map['album_id'] = $album_id;
				$parameter['album_id'] = $album_id;
				
				$this->assign('album_id',$album_id);
			}
			
			$this->assign('info',$info);
			$this->assign('flag',$flag);
			$this->assign('id',$id);
			
			$photo = M('Photos');
			$count      = $photo-> where($map)->count();
			$Page       = new Page($count,15,$parameter);
			$show       = $Page->show();
			
			$photolist = $photo->where($map)->limit($Page->firstRow.','.$Page->listRows)->order('created_at desc')->select();
			$this->assign('data',$photolist);
			$this->assign('page',$show);
			$this->display('Photos/list');
	    }
		
		
		public function uploadimg(){
			
			$upload = new \Think\Upload();   
			$upload->maxSize   =     3145728 ;
			$upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');
			$upload->savePath  =      '/images/'; 
			// 上传文件   js使用formdata 方式上传,formdata还真是 好用 啊 
			$info   =   $upload->uploadOne($_FILES['upload_file']);
			if(!$info) {// 上传错误提示错误信息
			    $this->show($upload->getError());
			}else{// 上传成功 获取上传文件信息
				// die('success...');
		    $path = '/Uploads'.$info['savepath'].$info['savename'];

		    $root = $this->getROOT();
	
		    $image = new \Think\Image(); 

				// 在图片左上角添加水印（水印文件位于./logo.png） 水印图片的透明度为50 并保存为water.jpg
				$image->open($root.$path);
				#$image->water($root.'/Public/images/water.png',\Think\Image::IMAGE_WATER_SOUTHEAST,80); 
				$image->save($root.$path);
				
			
		    $m = D('Photos');
		    $_POST['path'] = $path;
				if(!$m->create()){
					$this->show('error');
				}
				$id = $m->add();
				if($id>0){
					$result['id'] = $id;
					if($_POST['is_editer']){
						$result['path'] = __ROOT__.$path;
					}else {
						$result['path'] =$path;
					}
					$this->ajaxReturn($result);
					//$this->show($id);
				}else{
					$this->show('0');
				}
			}
			
			
		}
	}
?>
